﻿using Abp.Domain.Entities;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SportsPlatform.Global;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace SportsPlatform.BaseModels
{
    /// <summary>
    /// 通用数据库字段
    /// </summary>
    public class BaseModel : Entity<Guid>
    {
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public Guid CreateUserId { get; set; }

        /// <summary>
        /// 更改时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }

        /// <summary>
        /// 更改人
        /// </summary>
        public Guid? UpdateUserId { get; set; }

        /// <summary>
        /// 删除时间
        /// </summary>
        public DateTime? DeleteTime { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 删除人
        /// </summary>
        public Guid? DeleteUserId { get; set; }

        /// <summary>
        /// 添加
        /// </summary>
        public void Create(IHttpContextAccessor context)
        {
            CreateTime = DateTime.Now;
            CreateUserId = UldAdminSessionContext.GetCurrentUserInfo(context).Id;
            IsDeleted = false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        public void Update(IHttpContextAccessor context)
        {
            UpdateTime = DateTime.Now;
            UpdateUserId = UldAdminSessionContext.GetCurrentUserInfo(context).Id;
        }

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(IHttpContextAccessor context)
        {
            var user = UldAdminSessionContext.GetCurrentUserInfo(context);

            DeleteTime = DateTime.Now;
            IsDeleted = true;
            DeleteUserId = user.Id;
        }

        /// <summary>
        /// 编辑赋值
        /// </summary>
        /// <param name="context">HttpContext</param>
        public void Modify(IHttpContextAccessor context)
        {
            var user = UldAdminSessionContext.GetCurrentUserInfo(context);

            this.UpdateUserId = user.Id;
            this.UpdateTime = DateTime.Now;
        }
    }
}
